<?php
namespace App\SDK\PingAn\library;

class Request {
    /**
     * CURL按POST请求
     * @param string $url 请求地址
     * @param string|array $data 请求数据
     * @param array $header 请求头信息
     * @param int $timeout 请求超时设置
     * @return string 返回信息
     */
    public static function curlPost($url, $data, $header = [], $timeout = 30) {
        if (is_array($data)) {
            $data = http_build_query($data);
        }
        $ch = curl_init();
        $setopt = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADERFUNCTION => [self::class, 'readHeader'],
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $data
        ];
        if(strpos($url, 'https://') === 0) {
            $setopt[CURLOPT_SSL_VERIFYPEER] = false;
            $setopt[CURLOPT_SSL_VERIFYHOST] = false;
        }
        if(!empty($header)) {
            $setopt += [CURLOPT_HTTPHEADER => $header];
        }
        curl_setopt_array($ch, $setopt);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public static function curlPostFile($url, $data, $header = []){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if(strpos($url, 'https://') === 0) {
            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
        }
        if(!empty($header)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($curl, CURLOPT_HEADERFUNCTION, [self::class, 'readHeader']);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public static function curlDownFile($downUrl, $header = []) {
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $downUrl);
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        if(strpos($downUrl, 'https://') === 0) {
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
        }
        if(!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, [self::class, 'readHeader']);
        $file = curl_exec($ch);
        curl_close($ch);
        return $file;
    }

    public static function readHeader($curl, $headerLine) {
        $len = strlen($headerLine);
        $split = explode(':', $headerLine, 2);
        if (count($split) > 1) {
            $key = trim($split[0]);
            $value = trim($split[1]);
            $GLOBALS['G_HEADER'][$key] = $value;
        }
        $GLOBALS['G_HTTP_CODE'] = curl_getinfo($curl,CURLINFO_HTTP_CODE);
        return $len;
    }
}
